﻿var selectFileBtn_imgupload = "<a href=\"javascript:;\" rel-field=\"{field}\" rel-fieldtype=\"{fieldtype}\" class=\"btn blue file-select\" style=\"margin-right: 3px;\"> 选择已有文件 </a>";
var selectFileBtn_fileupload = "<a href=\"javascript:;\" rel-field=\"{field}\" rel-fieldtype=\"{fieldtype}\" class=\"input-group-addon btn blue file-select\"> 选择已有文件 </a>";
var selectFileBtn_multipleimgupload = "<a href=\"javascript:;\" rel-field=\"{field}\" rel-fieldtype=\"{fieldtype}\" class=\"btn blue file-select\" style=\"margin-right: 3px;\"> 选择已有文件 </a>";

//单图上传
$.each($(".imgupload-clear"), function (i, e) {
    var _this = $(this);
    var _this_filed = _this.attr("name").split('_')[1];
    _this.before(selectFileBtn_imgupload.replace("{field}", _this_filed).replace("{fieldtype}", "imgupload"));
});
//单文件上传
$.each($(".fileupload-clear"), function (i, e) {
    var _this = $(this);
    var _this_filed = _this.attr("name").split('_')[1];
    _this.before(selectFileBtn_fileupload.replace("{field}", _this_filed).replace("{fieldtype}", "fileupload"));
});
//相册上传
$.each($("[rel=Multiple-Img-Upload]"), function (i, e) {
    var _this = $(this);
    var _this_filed = _this.attr("name");
    _this.parent().after(selectFileBtn_multipleimgupload.replace("{field}", _this_filed).replace("{fieldtype}", "multipleimgupload"));
});

//点击选择
$(".file-select").on("click", function () {
    var _this = $(this);
    openFile(_this.attr("rel-field"), _this.attr("rel-fieldtype"));
});

function setSelectValue(field, fieldType, selectList) {
    if (fieldType == "multipleimgupload") {
        //这里要单独配置
        //layer.msg("相册");
        //渲染相册
        $.each(selectList, function (i, e) {
            $(".Multiple_" + field).prepend(MakeMultipleImg(e));
        });
        InitMultipleDrag();
        InitContextMenu();
    }
    if (fieldType == "fileupload") {
        //这里要单独配置
        //layer.msg("文件");
        //文件框
        $("input[field='" + field + "']").attr("data-value", selectList[0]);
        $("input[name=filesrc_" + field + "]").val(selectList[0]);
    }
    if (fieldType == "imgupload") {
        //这里要单独配置
        //layer.msg("普通图片");
        //图片框+图片显示容器
        $("input[field='" + field + "']").attr("data-value", selectList[0]);
        $("input[name=imgsrc_" + field + "]").val(selectList[0]);
        $("input[name=imgsrc_" + field + "]").parent().parent().find("img").attr("src", selectList[0]);
    }
}

function openFile(field, fieldType) {
    if (arguments.length != 2) {
        layer.msg("请检查参数是否正常");
        return false;
    }
    //iframe层-父子操作
    layer.open({
        title: "请选择",
        type: 2,
        area: ['980px', '550px'],
        fixed: true, //不固定
        maxmin: false,
        resize: false,
        scrollbar: false,
        content: appConfig.adminPath+'/Page_FileManager/FileList.aspx?field=' + field + '&fieldType=' + fieldType,
        btn: [' 确 定 ', ' 取 消 '],
        yes: function (index, layero) {

            //var selectList = window.parent.document.getElementById("layui-layer-iframe" + index).contentWindow.getSelectList();
            //var field = window.parent.document.getElementById("layui-layer-iframe" + index).contentWindow.field;
            //var fieldType = window.parent.document.getElementById("layui-layer-iframe" + index).contentWindow.fieldType;
            //if (selectList.length == 0) {
            //    window.parent.document.getElementById("layui-layer-iframe" + index).contentWindow.noSelectTip();
            //}
            //else {
            //    layer.close(index);
            //    //console.log(selectList, field, filedType);
            //    setSelectValue(field, fieldType, selectList);
            //}

            var selectList = window.document.getElementById("layui-layer-iframe" + index).contentWindow.getSelectList();
            var field = window.document.getElementById("layui-layer-iframe" + index).contentWindow.field;
            var fieldType = window.document.getElementById("layui-layer-iframe" + index).contentWindow.fieldType;
            if (selectList.length == 0) {
                window.document.getElementById("layui-layer-iframe" + index).contentWindow.noSelectTip();
            }
            else {
                layer.close(index);
                //console.log(selectList, field, filedType);
                setSelectValue(field, fieldType, selectList);
            }
        },
        btn2: function (index, layero) {
            //按钮【按钮二】的回调
            //console.log(index, layero);
            //return false 开启该代码可禁止点击该按钮关闭
        }
    });
}